package com.example.leetcode.niuke;

import java.util.*;

public class NiuKe11 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Integer count = Integer.parseInt(scanner.nextLine());
        String s = scanner.nextLine();
        String s1 = scanner.nextLine();
        String[] split1 = s1.split(",");
        Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
        String[] split = s.split(",");
        for (int i = 0;i<count;i++) {
            PriorityQueue<Integer> orDefault = map.getOrDefault(Integer.parseInt(split[i]), new PriorityQueue<>((x,y) ->Integer.parseInt( split1[y])-Integer.parseInt( split1[x])));
            orDefault.add(i);
            map.put(Integer.parseInt(split[i]),orDefault);
        }
        PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((x,y) ->{
            if (x[0] != y[0]) {
                return y[0] - x[0];
            }
            return y[1] - x[1];
        });

        for (Map.Entry<Integer,PriorityQueue<Integer>> entry:map.entrySet()) {
            if (entry.getValue().size() >= 3) {
                PriorityQueue<Integer> value = entry.getValue();
                int t = 0;
                for (int i = 0;i<3;i++) {
                    Integer poll = value.poll();
                    t += Integer.parseInt(split1[poll]);
                }
                priorityQueue.add(new int[]{t,entry.getKey()});
            }
        }
        int size =priorityQueue.size();
        for (int i = 0;i<size;i++) {
            int[] poll = priorityQueue.poll();
            System.out.print(poll[1]);
            if (i != size -1) {
                System.out.print(",");
            }
        }
    }
}
